package com.gem.controller;

import com.gem.domain.CCooperationResult;
import com.gem.domain.Teacher;
import com.gem.domain.User;
import com.gem.service.CCooperationResultService;
import com.gem.service.CCrCompanyService;
import com.gem.service.TeacherService;
import com.gem.service.UserService;
import com.gem.util.ConditionUtil;
import com.gem.util.JSONResultUtil;
import com.gem.util.MessageUtil;
import com.gem.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 合作成果控制器
 * create 2019/10/25 10:57
 * Modified By:
 */
@Controller
@SuppressWarnings("all")
@RequestMapping("/cr")
public class CCooperationResultController {

    //自动注入ConditionUtil
    private ConditionUtil cu;

    @Autowired
    public void setCu(ConditionUtil cu) {
        this.cu = cu;
    }

    //自动注入JSONResultUtil
    private JSONResultUtil result;

    @Autowired
    public void setResult(JSONResultUtil result) {
        this.result = result;
    }

    //自动注入CCooperationResultService
    private CCooperationResultService cCoopreationResultService;

    @Autowired
    public void setcCoopreationResultService(CCooperationResultService cCoopreationResultService) {
        this.cCoopreationResultService = cCoopreationResultService;
    }

    //自动注入UserService
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //自动注入TeacherService
    private TeacherService teacherService;

    @Autowired
    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    //自动注入CCrCompanyService
    private CCrCompanyService cCrCompanyService;

    @Autowired
    public void setcCrCompanyService(CCrCompanyService cCrCompanyService) {
        this.cCrCompanyService = cCrCompanyService;
    }

    //首页-浏览所有
    @RequestMapping("/crShow")
    public String crShow(Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            PageInfo<CCooperationResult> pageBean = cCoopreationResultService.selectCRByPage(currentPage, pageSize);
            if (pageBean != null && pageBean.getList().size() > 0) {
                model.addAttribute("pageBean", pageBean);
                List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(), currentPage);
                model.addAttribute("pageValue", pageValue);
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            model.addAttribute("pageSize", pageSize);
            return "company/result";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/home", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //查询显示
    @RequestMapping("/crSearchShow")
    public String crSearch(Integer comId, String ccrcDate, String ccrcTopic, String ccrcSales, String ccrcCourseTeacher, Model model,
                           @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                           @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            List<Teacher> teacherList = teacherService.findTeacherByName(ccrcCourseTeacher);
            if (teacherList.size() > 0) {
                Integer[] tIdArr = new Integer[teacherList.size()];
                for (int i = 0; i < teacherList.size(); i++) {
                    tIdArr[i] = teacherList.get(i).getTId();
                }
                cu.setCcrcCourseTeacherArr(tIdArr);
            }
            List<User> userList = userService.findMmembersBySalesName(ccrcSales);
            if (userList.size() > 0) {
                Integer[] salesUidArr = new Integer[userList.size()];
                for (int i = 0; i < userList.size(); i++) {
                    salesUidArr[i] = userList.get(i).getUid();
                }
                cu.setCcrcSalesArr(salesUidArr);
            }
            Integer[] crIdArr = cCrCompanyService.selectCCByccCoIdService(comId);
            if (crIdArr.length > 0) {
                cu.setCcrcCrIdArr(crIdArr);
            }
            cu.setCcrcCourseTeacher(ccrcCourseTeacher);
            cu.setCcrcComId(comId);
            cu.setCcrcSales(ccrcSales);
            cu.setCcrcDate(ccrcDate);
            cu.setCcrcTopic(ccrcTopic);
            if (!((ccrcCourseTeacher != null && teacherList.size() == 0) || (ccrcSales != null && userList.size() == 0))) {
                PageInfo<CCooperationResult> pageBean = cCoopreationResultService.selectCRByConditionByPage(currentPage, pageSize, cu);
                if (comId != null && crIdArr.length <= 0) {
                    model.addAttribute("info", "暂无信息记录");
                } else if (pageBean != null && pageBean.getList().size() > 0) {
                    List<Integer> pageValue = PageUtil.getPageValue(pageBean.getPages(), currentPage);
                    model.addAttribute("pageValue", pageValue);
                    model.addAttribute("pageBean", pageBean);
                } else {
                    comId=null;
                    model.addAttribute("info", "暂无信息记录");
                }
            } else {
                model.addAttribute("info", "暂无信息记录");
            }
            model.addAttribute("ccrc", cu);
            model.addAttribute("comId", comId);
            model.addAttribute("pageSize", pageSize);
            model.addAttribute("searchURL", "/cr/crSearchShow?" + "ccrcCourseTeacher=" + ccrcCourseTeacher + "&ccrcSales=" + ccrcSales + "&ccrcDate=" + ccrcDate + "&ccrcTopic=" + ccrcTopic);//分页带参数用
            return "company/result";
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/cr/crShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //评价
    @RequestMapping(value = "/crEvaluation", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil crEvaluation(String crId, String crCourseGrade, String crCourseEvaluation) {
        try {
            CCooperationResult cCooperationResult = cCoopreationResultService.selectCRByIdService(Integer.parseInt(crId));
            if (cCooperationResult != null) {
                if (crCourseGrade.equals(cCooperationResult.getCrCourseGrade()) && crCourseEvaluation.equals(cCooperationResult.getCrCourseEvaluation())) {
                    result.setId(1);
                    result.setData("未作新评价哦！");
                } else {
                    if ((cCooperationResult.getCrCourseGrade() != null || cCooperationResult.getCrCourseEvaluation() != null) && crCourseGrade.equals("0") && crCourseEvaluation.equals("暂无评价哦！")) {
                        cCooperationResult.setCrCourseGrade("0");
                        cCooperationResult.setCrCourseEvaluation("暂无评价哦！");
                    } else {
                        cCooperationResult.setCrCourseGrade(crCourseGrade);
                        cCooperationResult.setCrCourseEvaluation(crCourseEvaluation);
                    }
                    if (cCoopreationResultService.modifyCRService(cCooperationResult)) {
                        if ((cCooperationResult.getCrCourseGrade() != null || cCooperationResult.getCrCourseEvaluation() != null) && crCourseGrade.equals("0") && crCourseEvaluation.equals("暂无评价哦！")) {
                            result.setId(0);
                            result.setData("评价已删除哦！");
                        } else {
                            result.setId(0);
                            result.setData("评价成功！谢谢你的评价");
                        }
                    } else {
                        result.setId(-1);
                        result.setData("服务器异常，评价失败哦！");
                    }
                }
            } else {
                result.setId(-1);
                result.setData("服务器异常，评价失败哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //单个删除
    @RequestMapping(value = "/crDelete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil crDelete(String crId, int currentPageSize, int currentPage, int lastPage) {
        try {
            if (crId != null && !crId.trim().equals("")) {
                int cr_Id = Integer.parseInt(crId);
                if (cCoopreationResultService.deleteCRByIdService(cr_Id)) {
                    if (currentPageSize == 1 && currentPage == lastPage) {
                        result.setId(0);
                    } else {
                        result.setId(1);
                    }
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("服务器异常，删除失败哦！");
                }
            } else {
                result.setId(-1);
                result.setData("服务器异常，没有提交编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //批量删除
    @RequestMapping(value = "/crsDelete", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil crsDelete(String crIdStr, int currentPageSize, int currentPage, int lastPage) {
        try {
            String[] crIdArr = crIdStr.split(",");
            if (crIdArr.length != 0) {
                if (cCoopreationResultService.deleteCRByIdsService(crIdArr)) {
                    if (crIdArr.length == currentPageSize && currentPage == lastPage) {
                        result.setId(0);
                    } else {
                        result.setId(1);
                    }
                    result.setData("删除成功！");
                } else {
                    result.setId(-1);
                    result.setData("删除发生异常，请检查服务器！");
                }
            } else {
                result.setId(-1);
                result.setData("没有提交编号参数哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }

    //添加页面
    @RequestMapping("/crAddUI")
    public String crAddUI(Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            List<User> userList = userService.findEmembers();
            List<Teacher> teacherList = teacherService.selectAllTeacher();
            if (userList.size() > 0 && teacherList.size() > 0) {
                model.addAttribute("salesList", userList);
                model.addAttribute("teacherList", teacherList);
                model.addAttribute("currentPage", currentPage);
                model.addAttribute("pageSize", pageSize);
                return "company/result_insert";
            } else {
                MessageUtil.returnInfoPage("3", "/ds/dsShow", "暂无销售专员或讲师信息哦！", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/ds/dsShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //添加功能
    @RequestMapping("/crAdd")
    public String crAdd(CCooperationResult cCooperationResult, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            if (cCoopreationResultService.addCRService(cCooperationResult)) {
                MessageUtil.returnInfoPage("3", "/cr/crShow?currentPage=" + currentPage + "&pageSize=" + pageSize, "添加成功！", model);
            } else {
                MessageUtil.returnInfoPage("3", "/cr/crShow?currentPage=" + currentPage + "&pageSize=" + pageSize, "添加失败了哟！", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/cr/crShow", "服务器异常，请联系管理员哦！", model);
        }
        return "message";
    }

    //修改页面
    @RequestMapping("/crModifyUI")
    public String crModifyUI(String crId, Integer comId, String ccrcDate, String ccrcTopic, String ccrcSales, String ccrcCourseTeacher, Model model, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            CCooperationResult cCooperationResult = cCoopreationResultService.selectCRByIdService(Integer.parseInt(crId));
            List<User> userList = userService.findEmembers();
            List<Teacher> teacherList = teacherService.selectAllTeacher();
            if (cCooperationResult != null && userList.size() > 0 && teacherList.size() > 0) {
                model.addAttribute("cr", cCooperationResult);
                model.addAttribute("salesList", userList);
                model.addAttribute("teacherList", teacherList);
                model.addAttribute("currentPage", currentPage);
                model.addAttribute("pageSize", pageSize);
                model.addAttribute("comId", comId);
                model.addAttribute("ccrcDate", ccrcDate);
                model.addAttribute("ccrcTopic", ccrcTopic);
                model.addAttribute("ccrcSales", ccrcSales);
                model.addAttribute("ccrcCourseTeacher", ccrcCourseTeacher);
                return "company/result_modify";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        MessageUtil.returnInfoPage("3", "/cr/crShow", "服务器异常，请联系管理员哦！", model);
        return "message";
    }

    //修改功能
    @RequestMapping(value = "/crModify", method = RequestMethod.POST)
    @ResponseBody
    public JSONResultUtil crModify(CCooperationResult cCooperationResult) {
        try {
            if (cCoopreationResultService.modifyCRService(cCooperationResult)) {
                result.setId(0);
                result.setData("修改成功！");
            } else {
                result.setId(1);
                result.setData("修改未成功哦，请重试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setId(-1);
            result.setData("服务器异常，请联系管理员哦！");
        }
        return result;
    }
}
